/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.commons.scripting.internal;

/**
 * Based on scripting language from Media Solutions Slovakia, a.s.
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class EOLParser {
	
	private enum State {
		START, WIN_OR_MAC
	}

	private State state = State.START;
	
	/*package*/ EOLParser() {
		// limiting instantiation to this package
	}
	
	boolean isEOL(char nextCharacter) {
		switch (state) {
		case START:
			if ('\r' == nextCharacter) {
				state = State.WIN_OR_MAC;
				return true;	// it is EOL, but we don't know if WIN of MAC EOL, therefore WIN_OR_MAC state
			} else if ('\n' == nextCharacter) {
				return true;	// Linux EOL
			}
			break;
		case WIN_OR_MAC:
			if ('\n' == nextCharacter) {
				state = State.START;
				return false;	// WINDOWS EOL, but we have yielded eol in previous state
			}
			state = State.START;	// MAC EOL in previous state
			break;
		}
		return false;	// no EOL detected
	}
}
